﻿using System.Collections.Generic;
using System.Linq;
using HHPS.CreditEducation.DataProvider;
using HHPS.CreditEducation.Frameworks.Constants;

namespace HHPS.CreditEducation.Business
{
    public partial class Business
    {
        public List<Syllabus> GetListSyllabusByMajorId(int majorId)
        {
            return DataContex != null ? DataContex.Syllabuses.Where(s => s.IdMajor == majorId).ToList() : null;
        }

        public bool AddNewSyllabusValidator(Syllabus syllabus, out string message)
        {
            if (DataContex.Syllabuses.Any(f => f.IdMajor == syllabus.IdMajor && f.IdSubject == syllabus.IdSubject))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, "Khung chương trình");
                return false;
            }

            message = string.Empty;
            return true;
        }

        public void AddNewSyllabus(Syllabus syllabus)
        {
            DataContex.Syllabuses.AddObject(syllabus);
            DataContex.SaveChanges();
        }

        public void DeleteSyllabuses(List<Syllabus> syllabuses)
        {
            for (int i = syllabuses.Count - 1; i >= 0; i--)
            {
                int id = syllabuses[i].Id;
                Syllabus deleteItem = DataContex.Syllabuses.Single(f => f.Id == id);
                DataContex.Syllabuses.DeleteObject(deleteItem);
            }
            DataContex.SaveChanges();
        }
    }
}